import logging

import pytest

from resource.pom.settings import Settings
from utils.manager import Direction, Images


class Test:
    app = Settings

    @pytest.mark.parametrize('setup_teardown', [app.bundle_name], indirect=True)
    def test(self, setup_teardown, device):
        logging.info('步骤1.进入桌面，点击设置-辅助功能，打开高对比度文字开关或关闭开关')
        device.start_ability(self.app.bundle_name, self.app.ability_name)
        device.dirc_fling(Direction.Down)
        device.click(*self.app.accessibility_features_item)
        device.refresh_layout()
        font_switch = device.get_element_by_type('Toggle')
        font_area = device.get_element_area(font_switch)
        font_switch_status0 = font_switch.get('checked')
        # switch0 = device.save_snapshot_to_local('font_switch0.jpeg')
        # switch0 = Images.crop_picture(switch0, *font_area)

        device.click_element(font_switch)
        device.refresh_layout()
        font_switch_status1 = device.get_element_by_type('Toggle').get('checked')
        switch1 = device.save_snapshot_to_local('font_switch1.jpeg')
        switch1 = Images.crop_picture(switch1, *font_area)
        device.click_element(font_switch)
        device.refresh_layout()
        font_switch_status2 = device.get_element_by_type('Toggle').get('checked')
        switch2 = device.save_snapshot_to_local('font_switch2.jpeg')
        switch2 = Images.crop_picture(switch2, *font_area)

        logging.info('预期结果1.开关能正常开启、关闭')
        assert font_switch_status1 != font_switch_status0, '高对比度文字开关或关闭开关状态切换失败'
        assert font_switch_status2 == font_switch_status0, '高对比度文字开关或关闭开关状态切换失败'
        similarity12 = Images.compare_image_similarity(switch1, switch2)
        assert similarity12 < 0.9, '界面上显示的开关状态未发生变化'

        logging.info('步骤2.打开颜色反转开关')
        color_switch = device.get_element_by_type('Toggle', index=1)
        color_switch_status0 = color_switch.get('checked')
        device.click_element(color_switch)
        device.refresh_layout()
        color_switch_status1 = device.get_element_by_type('Toggle', index=1).get('checked')
        device.click_element(color_switch)
        device.refresh_layout()
        color_switch_status2 = device.get_element_by_type('Toggle', index=1).get('checked')
        logging.info('预期结果2.开关能正常开启、关闭')
        assert color_switch_status1 != color_switch_status0, '颜色反转开关状态切换失败'
        assert color_switch_status2 == color_switch_status0, '颜色反转开关状态切换失败'
